#!/usr/bin/env bash
set -e

TARGET_PATH=$1

if [ -z "${TARGET_PATH}" ];then
  TARGET_PATH="/app"
fi

echo "Start setup runtime public url"
echo "Runtime Public url = ${RUNTIME_PUBLIC_URL}"
safe_runtime_public_url=$(printf '%s' "${RUNTIME_PUBLIC_URL}" | sed 's/\//\\\//g')

for file_name in `grep __RUNTIME_PUBLIC_URL__ -rl --include=\*.{html,js,css,json} ${TARGET_PATH}`
do
  case $(uname) in
  Darwin)
    sed -i .bak "s/\/__RUNTIME_PUBLIC_URL__/${safe_runtime_public_url}/g" "${file_name}"
    ;;
  Linux)
    sed -i "s/\/__RUNTIME_PUBLIC_URL__/${safe_runtime_public_url}/g" "${file_name}"
    ;;
  esac
done
